package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    MemberService memberService;

    @Reference
    SetmealService setmealService;


    @Reference
    ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map<String,Object> map = memberService.getMemberReport();
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getMemeberReportByDate")
    public Result getMemeberReportByDate(String date){
        String[] split = date.split(",");
        String start = split[0];
        String end = split[1];
        System.out.println();
        try {
            Map<String,Object> map = memberService.getMemberReportByDate(start,end);
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }


    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            /**



            setmealCount : [
                     {value: 10, name: a},
                     {value: 10, name: a},
                     {value: 10, name: a}
                ]
             */
            List<Map<String,String>> setmealCount = setmealService.getSetmealReport();
            //存储套餐的名称
            List<String> setmealNames = new ArrayList<>();
            //取出套餐的名称
            for (Map<String, String> map : setmealCount) {
                String name = map.get("name");
                setmealNames.add(name);
            }

            //创建返回值map集合
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("setmealNames",setmealNames );
            resultMap.put("setmealCount",setmealCount );

            return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, resultMap);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }


    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map<String,Object> map = reportService.getBusinessReportData();
            return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_BUSINESS_REPORT_FAIL);
        }
    }

    /**
     * 导出运营数据
         * reportDate:null,  统计日期（今天）
         todayNewMember :0,  今日新增会员数
         totalMember :0,     总会员数
         thisWeekNewMember :0,  本周新增会员数,
         thisMonthNewMember :0,  本月新增会员数
         todayOrderNumber :0,     今日预约数（有
         todayVisitsNumber :0,     今日到诊数（
         thisWeekOrderNumber :0,   本周预约数（
         thisWeekVisitsNumber :0,  本周到诊数（
         thisMonthOrderNumber :0,   本月预约数
         thisMonthVisitsNumber :0,  本月到诊数
         hotSetmeal :[  热门套餐（查询前三个套餐）
     */
    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response){
        try {
            //获取数据
            Map<String,Object> map = reportService.getBusinessReportData();
            //取出数据
            String reportDate = String.valueOf( map.get("reportDate"));
            long todayNewMember = Long.parseLong(String.valueOf( map.get("todayNewMember")));
            long totalMember = Long.parseLong(String.valueOf( map.get("totalMember")));
            long thisWeekNewMember = Long.parseLong(String.valueOf( map.get("thisWeekNewMember")));
            long thisMonthNewMember = Long.parseLong(String.valueOf( map.get("thisMonthNewMember")));
            long todayOrderNumber = Long.parseLong(String.valueOf( map.get("todayOrderNumber")));
            long todayVisitsNumber = Long.parseLong(String.valueOf( map.get("todayVisitsNumber")));
            long thisWeekOrderNumber = Long.parseLong(String.valueOf( map.get("thisWeekOrderNumber")));
            long thisWeekVisitsNumber = Long.parseLong(String.valueOf( map.get("thisWeekVisitsNumber")));
            long thisMonthOrderNumber = Long.parseLong(String.valueOf( map.get("thisMonthOrderNumber")));
            long thisMonthVisitsNumber = Long.parseLong(String.valueOf( map.get("thisMonthVisitsNumber")));
            List<Map<String,Object>>  hotSetmeal = (List<Map<String, Object>>) map.get("hotSetmeal");

            //获取模板文件流对象
            InputStream inputStream = ReportController.class.getResourceAsStream("/template/report_template.xlsx");
            //创建工作薄对象
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            //获取工作表
            XSSFSheet sheet = workbook.getSheetAt(0);

            //统计日期
            //获取row
            XSSFRow row = sheet.getRow(2);
            //获取单元格
            XSSFCell cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(reportDate);

            //新增会员数
            //获取row
            row = sheet.getRow(4);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(todayNewMember);

            //总会员数
            //获取row
            row = sheet.getRow(4);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(totalMember);


            //本周新增会员数
            //获取row
            row = sheet.getRow(5);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(thisWeekNewMember);

            //本月新增会员数
            //获取row
            row = sheet.getRow(5);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(thisMonthNewMember);

            //今日预约数
            //获取row
            row = sheet.getRow(7);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(todayOrderNumber);


            //今日到诊数
            //获取row
            row = sheet.getRow(7);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(todayVisitsNumber);


            //本周预约数
            //获取row
            row = sheet.getRow(8);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(thisWeekOrderNumber);

            //本周到诊数
            //获取row
            row = sheet.getRow(8);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(thisWeekVisitsNumber);


            //本月预约数
            //获取row
            row = sheet.getRow(9);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(thisMonthOrderNumber);

            //新增会员数
            //获取row
            row = sheet.getRow(9);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(thisMonthVisitsNumber);


            int rownum = 12;
            //设置热门套餐
            for (Map<String, Object> setmeal : hotSetmeal) {
                //获取row
                row = sheet.getRow(rownum);
                //获取单元格
                cell = row.getCell(4);
                cell.setCellValue(String.valueOf(setmeal.get("name")));
                cell = row.getCell(5);
                cell.setCellValue(String.valueOf(setmeal.get("setmeal_count")));
                cell = row.getCell(6);
                cell.setCellValue(String.valueOf(setmeal.get("proportion")));
                rownum ++;
            }

            // 通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename="+reportDate+"_report.xlsx");
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
